
package xj.graph2d.app.project;

import static xj.app.BaseApplicationFrame.getResourceString;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.File;
import java.text.MessageFormat;

import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import xj.graph2d.Document;
import xj.graph2d.SimpleDrawDocument;
import xj.graph2d.app.DrawAction;
import xj.graph2d.app.DrawApp;
import xj.graph2d.dialog.DialogPanel;
import xj.graph2d.dialog.TitledDialogPanel;

public class NewStencilListener 
  extends DrawAction 
  implements ActionListener {

  class StencilOptionPanel 
    extends DialogPanel {

    public StencilOptionPanel(String name) {
      super(name);


      JLabel nameLabel = new JLabel(getResourceString("StencilName.text") + " ");
      nameField = new JTextField();

      JLabel authorLabel = new JLabel(getResourceString("Author.text") + " ");
      authorField = new JTextField();
      JLabel titleLabel = new JLabel(getResourceString("Title.text") + " ");
      titleField = new JTextField();
      JLabel orgLabel = new JLabel(getResourceString("Organization.text") + " ");
      orgField = new JTextField();

      JLabel descriptionLabel = new JLabel(getResourceString("Description.text")  + " ");
      descriptionLabel.setVerticalAlignment(SwingConstants.TOP);
      descriptionField = new JTextArea(4, 40);
      descriptionField.setBorder(BorderFactory.createEtchedBorder());

      JLabel typeLabel = new JLabel(getResourceString("StencilType.text") + " ");
      typeField = new JComboBox();
      for (int i = 0; i < Document.DocumentTypeNames.length; i++) {
	String display = 
	  getResourceString("DiagramType." + 
					    Document.DocumentTypeNames[i] + ".text");
	Document.DocumentType type = Document.toDocumentType(Document.DocumentTypeNames[i]);

	typeField.addItem(new DocTypeItem(display, type));
      }
      typeField.addItemListener(new ItemListener() {
	  public void itemStateChanged(ItemEvent e) {
	    if (e != null && 
		e.getStateChange() == ItemEvent.SELECTED) { 
	      setOption();
	    }
	  }
	});

      addRow(nameLabel, nameField);
      addRow(typeLabel, typeField);
      addRow(titleLabel, titleField);
      addRow(authorLabel, authorField);
      addRow(orgLabel, orgField);
      addRow(descriptionLabel, new JScrollPane(descriptionField));

      setPreferredSize(new Dimension(400, 330));
      //setOption();
    }

    @Override
    public void setOption() {
      DocTypeItem item = (DocTypeItem) typeField.getSelectedItem();
      Document.DocumentType type = item.type; 
      String newname = Document.getDefaultFilename(type) + "Stencil";
      String ext = "." + DrawApp.stencilFileExtension;
      newname = getNewStencilName(newname, ext);
      nameField.setText(newname);
    }

    @Override
    public void applyOption() {
      String name = nameField.getText();
      if (name != null) {
	name = name.trim();
      }
      DocTypeItem item = (DocTypeItem) typeField.getSelectedItem();
      Document.DocumentType type = item.type; 

      if (name != null) {
	String ext = "." + DrawApp.stencilFileExtension;
	String filename = stencilDir + File.separator + name + ext;
	File file = new File(filename);
	boolean exists = file.exists();

	if (exists) { 
	  String title, message;
	  title = getResourceString("NewStencil.Error.title");
	  message = getResourceString("NewStencil.Error.message");
	  message = MessageFormat.format(message, name);
	  JOptionPane.showMessageDialog(main.getJFrame(), 
					message, title, 
					JOptionPane.ERROR_MESSAGE);
	} else { 
	  main.newStencil(name, filename, type);
	  SimpleDrawDocument doc = main.getDoc();
	  if (doc != null) {
	    String title = titleField.getText();
	    String author = authorField.getText();
	    String org = orgField.getText();
	    String des = descriptionField.getText();
	    doc.setAuthor(author);
	    doc.setTitle(title);
	    doc.setOrganization(org);
	    doc.setDescription(des);
	  }
	}
      }
    }

    protected JTextField nameField;

    protected JComboBox typeField;

    protected JTextField authorField;

    protected JTextField titleField;

    protected JTextField orgField;

    protected JTextArea descriptionField;

  }

  public String getNewStencilName(String prefix, String ext) {
    if (prefix == null) {
      prefix = "NewStencil";
    }
    int i = 1;
    String newItemName = (prefix + i);
    while (true) {
      String filename = stencilDir + File.separator + newItemName;
      if (ext != null) {
	filename += ext;
      }
      File file = new File(filename);
      if (!file.exists()) { 
	break;
      }
      newItemName = (prefix + (++i));
    }
    return newItemName;
  }

  protected TitledDialogPanel stencilOptionDialog = null;
  protected StencilOptionPanel stencilPanel = null;
  protected String stencilDir = null;

  public void actionPerformed(ActionEvent e) {
    if (stencilOptionDialog == null) {

      String userhome = System.getProperty("user.home");
      String appDir = main.getAppDir();
      stencilDir = userhome + File.separator + appDir + File.separator + DrawApp.STENCIL_DIR;

      //String newStencilPanelTitle = getResourceString("Item.title");
      String newStencilDialogTitle = getResourceString("NewStencilDialog.title");
      String newStencilHeader = getResourceString("NewStencilDialog.header");
      String[] newStencilText = { 
	getResourceString("NewStencilDialog.text1"),
	getResourceString("NewStencilDialog.text2"),	
      };
      Icon icon = DrawApp.getIcon("NewWizIcon");

      DialogPanel[] options = { 
	stencilPanel = new StencilOptionPanel(null), //newStencilPanelTitle),
      };

      stencilOptionDialog = new TitledDialogPanel(main.getJFrame(), 
						  JOptionPane.OK_CANCEL_OPTION, 
						  null, options);
      stencilOptionDialog.setTitle(newStencilDialogTitle);
      stencilOptionDialog.setTitlePaneText(newStencilHeader, newStencilText, icon);
    }

    stencilOptionDialog.setOptions();
    stencilOptionDialog.showOptionDialog();
  }

}
